<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/21
 * Time: 11:10
 */

namespace App\Http\Logics\MiniProgramAdmin;

use App\Http\Logics\BaseLogic;
use App\Order;

class OrderLogic extends BaseLogic
{
    public static function deliver($data)
    {
        $order = Order::find($data['order_id']);
        if (empty($order)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $order->courier_number = $data['courier_number'];
        $order->status = Order::HAS_DELIVER;
        if (!$order->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function dhcx($data)
    {
        $query_data = [
            'number' => $data['courier_number'],
            'type' => 'auto'
        ];

        // 获取快递物流信息
        $appcode = "b38de016e7c24ba8b9e3d478b18cbfa4";
        $headers = [
            "Authorization:APPCODE " . $appcode
        ];
        $url = "http://jisukdcx.market.alicloudapi.com/express/query?".http_build_query($query_data);
        $result = self::httpGet($url, $headers);
        if (objectToArray(json_decode($result->data))['status']){
            return self::response(self::SUCCESS);
        }
        $result_data_arr = objectToArray(json_decode($result->data));

        // 请求快递公司数据
        $url2 = 'http://jisukdcx.market.alicloudapi.com/express/type';
        $result2 = self::httpGet($url2, $headers);
        $result2_data_arr = objectToArray(json_decode($result2->data));

        // 给物流信息加上快递名称
        foreach ($result2_data_arr['result'] as $item){
            if ($item['type'] == strtoupper($result_data_arr['result']['type'])){
                $result_data_arr['result']['name'] = $item['name'];
                break;
            }
            continue;
        }
        return self::response(self::SUCCESS, $result_data_arr['result']);
    }

    public static function getOrderList($data)
    {
        $response = self::getUserIdByLoginSession($data['loginSession']);
        if ($response->code) {
            return self::response($response->code);
        }
        $user_id = $response->data;
        $query = Order::with(['goods', 'goods.product'])->where(function($query) use ($data, $user_id){
            if (isset($data['cafe_id']) && $data['cafe_id']){
                $query->whereHas('goods.product', function ($query) use ($data, $user_id){
                    $query->where('product.cafe_id', $data['cafe_id'])->where('product.user_id', $user_id);
                });
            }
            if (isset($data['partner_id']) && $data['partner_id']){
                $query->whereHas('goods.product', function ($query) use ($data, $user_id){
                    $query->where('product.partner_id', $data['partner_id'])->where('product.user_id', $user_id);
                });
            }
            $query->where('status', $data['status']);
        });
        $order = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
        if (empty($order)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $order);
    }



    public static function refund($data)
    {
        $order = Order::where(function ($query){
            $query->whereIn('status', Order::$canRefund);
        })->find($data['order_id']);
        if (empty($order)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $order->refund_number = strtoupper('NO'.date('Y').date('m').date('d').date('h').date('i').randomStr(6));
        $order->refund_time = time();
        // 微信退款
        $app = app('wechat.payment');
        $result = $app->refund->byOutTradeNumber($order->no, $order->refund_number, $order->total_money*100, $order->total_money*100);
        // 如果退款成功则将订单状态改为已退款
        if ($result['return_code'] == 'SUCCESS' && $result['return_msg'] == 'OK'){
            $order->status = Order::HAS_REFUND;
        }
        if (!$order->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }
}